package scut.facerecognition.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import scut.facerecognition.entity.dto.CommentDTO;
import scut.facerecognition.entity.dto.PageResult;
import scut.facerecognition.entity.pojo.User;
import scut.facerecognition.entity.pojo.PostComment;
import scut.facerecognition.mapper.UserMapper;
import scut.facerecognition.mapper.PostCommentMapper;
import scut.facerecognition.mapper.PostMapper;
import scut.facerecognition.service.CommentService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 评论服务实现类
 */
@Service
public class CommentServiceImpl implements CommentService {
    
    @Autowired
    private PostCommentMapper commentMapper;
    
    @Autowired
    private PostMapper postMapper;
    
    @Autowired
    private UserMapper userMapper;
    
    /**
     * 日期时间格式化器
     */
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    @Override
    public PageResult<CommentDTO> getCommentList(Long postId, int page, int size) {
        // 创建分页对象
        Page<PostComment> pageParam = new Page<>(page, size);
        
        // 查询一级评论
        IPage<PostComment> commentPage = commentMapper.selectRootComments(pageParam, postId);
        
        // 转换为DTO列表
        List<CommentDTO> commentDTOList = new ArrayList<>();
        for (PostComment comment : commentPage.getRecords()) {
            CommentDTO dto = convertToDTO(comment);
            
            // 获取子评论
            List<PostComment> childComments = commentMapper.selectChildComments(comment.getId());
            List<CommentDTO> childDTOList = new ArrayList<>();
            for (PostComment childComment : childComments) {
                childDTOList.add(convertToDTO(childComment));
            }
            dto.setChildren(childDTOList);
            
            commentDTOList.add(dto);
        }
        
        // 返回分页结果
        return new PageResult<>(
                commentDTOList,
                commentPage.getTotal(),
                commentPage.getCurrent(),
                commentPage.getSize()
        );
    }
    
    @Override
    @Transactional
    public Long addComment(PostComment comment) {
        // 设置初始值
        comment.setStatus(1);
        comment.setAddTime(LocalDateTime.now());
        
        // 保存到数据库
        commentMapper.insert(comment);
        
        // 更新帖子评论数
        postMapper.incrementCommentCount(comment.getPostId());
        
        return comment.getId();
    }
    
    /**
     * 将评论实体转换为DTO
     * @param comment 评论实体
     * @return 评论DTO
     */
    private CommentDTO convertToDTO(PostComment comment) {
        CommentDTO dto = new CommentDTO();
        dto.setId(comment.getId());
        dto.setContent(comment.getContent());
        dto.setUserId(comment.getUserId());
        dto.setParentId(comment.getParentId());
        dto.setAddTime(comment.getAddTime().format(DATE_FORMATTER));
        
        // 获取用户信息
        User user = userMapper.selectById(comment.getUserId());
        if (user != null) {
            dto.setUserNickname(user.getNickname());
            dto.setUserPhoto(user.getPhoto());
        }
        
        return dto;
    }
}
